//
//  NSKUserInfo.swift
//  Preferential
//
//  Created by Fineland on 2021/3/18.
//

class NSKUserInfo: NSObject {
    
    //实例
    static let shared = NSKUserInfo()
    //保存文件
    private let USER_FILE_NAME: String = "lgu.dta"
    
    //个人信息
    var loginUser: NSKLoginUserModel!
    
    //为上防止调用init方法
    private override init() {
        
    }
    
    class func isLogin() -> Bool {
        
        //当前对象
        if let tempObj = self.shared.loginUser {
            return tempObj.isLogined > 0
        }
        //返回
                return false
        return true
    }
    
    func logout() -> Void {
        
        //登录标志
        loginUser.isLogined = 0
        //保存
        save()
    }
    
    func read() -> Void {
        
        //读取本地信息
        let userInfoPath = filePath(USER_FILE_NAME)
        if let tempData = NSDictionary(contentsOfFile: userInfoPath) as? Dictionary<String, Any> {
            loginUser = tempData.toModel(NSKLoginUserModel.self)
        }
    }
    
    func save()-> Void {
        
        //本地路径
        let pathVal = filePath(USER_FILE_NAME)
        //保存信息
        let tempVal = NSDictionary(dictionary: loginUser.dictionary()).write(toFile: pathVal, atomically: true)
        if true != tempVal {
            #if DEBUG
            print("用户信息保存失败")
            #endif
        }
    }
    
    private func filePath(_ file: String) -> String {
        
        //文件路径
        let pathArray = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
        let filePath = pathArray.first!.appending("/" + file)
        //返回
        return filePath
    }
}

// MARK: -

class NSKLoginUserModel: Codable {
    
//    /// 登录帐号
//    var account: String
//    //登录密码
//    var password: String
//    /// token
//    var token: String
//    /// 用户id
//    var id: Int
//    /// 店铺
    var store: NSKUserStoreModel?
    /// 是否登录？
    var isLogined: Int = 0
    
    /// 17337107036
        var token: String
        /// 3
        var permissionid: String
        /// 0
        var companymode: String
        /// 郑州聚时物流有限公司
        var companyname: String
        ///
        var arriveaddress: String
        /// 15838081347
        var query_phone: String
        ///
        var creat_date: String
        ///
        var headimgurl: String
        /// 17737312394
        var finance_phone: String
        /// 196
        var printlabelid: String
        ///
        var publicCode: String
        /// 17337107036
        var telephone: String
        /// 刘
        var username: String
        ///
        var pay_useful_life: String
        /// 0
        var areabranchid: String
        /// 0
        var rate_dsprice: String
        /// 2
        var branchid: Int = 0
        /// 0
        var createpage: String
        /// 2
        var branch_pay: String
        /// 聚时物流
        var companyname_d: String
        /// 2
        var printpage: String
        /// 0
        var allpage: String
        /// 2
        var printpageid: String
        /// 1107036
        var uid: String
        /// 1
        var createpageid: String
        /// 广州
        var branchcity: String
        /// 1
        var companyid: Int = 0
        ///
        var address: String
        /// 0
        var retCode: Int = 0
        /// 1
        var printlabel: String
        /// 15821508096
        var companyphone: String
        /// 0
        var bj_price_rate: String
    }

// MARK: -

struct NSKUserStoreModel: Codable {
    
    /// 店铺名称
    var name: String
    /// 店铺id
    var id: Int
    /// 店铺图片(相对路径)
    var logo: Optional<String>
    /// 店铺状态:0-歇业中;1-营业中;2-已过期
    var status: Int
    /// 是否有多家店铺(false的话，点击切换店铺的时候提示，“您目前只有一家店铺”)
    var isMoreStore: Bool
}
